package com.castle.fortress.admin.goods.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.castle.fortress.admin.core.constants.GlobalConstants;
import com.castle.fortress.admin.goods.dto.GoodsCategoryDto;
import com.castle.fortress.admin.goods.entity.GoodsCategoryEntity;
import com.castle.fortress.admin.goods.service.GoodsCategoryService;
import com.castle.fortress.common.entity.RespBody;
import com.castle.fortress.common.exception.BizException;
import com.castle.fortress.common.respcode.GlobalRespCode;
import com.castle.fortress.common.utils.ConvertUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品分类表 api 控制器
 *
 * @author majunjie
 * @since 2021-11-22
 */
@Api(tags="商品分类表api管理控制器")
@RestController
@RequestMapping("/api/goods/goodsCategory")
public class ApiGoodsCategoryController {
    @Autowired
    private GoodsCategoryService goodsCategoryService;


    /**
     * 商品分类表的分页展示
     * @param goodsCategoryDto 商品分类表实体类
     * @param currentPage 当前页
     * @param size  每页记录数
     * @return
     */
    @ApiOperation("商品分类表分页展示")
    @GetMapping("/page")
    public RespBody<IPage<GoodsCategoryDto>> pageGoodsCategory(GoodsCategoryDto goodsCategoryDto, @RequestParam(required = false) Integer currentPage, @RequestParam(required = false)Integer size){
        Integer pageIndex= currentPage==null? GlobalConstants.DEFAULT_PAGE_INDEX:currentPage;
        Integer pageSize= size==null? GlobalConstants.DEFAULT_PAGE_SIZE:size;
        Page<GoodsCategoryDto> page = new Page(pageIndex, pageSize);

        IPage<GoodsCategoryDto> pages = goodsCategoryService.pageGoodsCategory(page, goodsCategoryDto);
        return RespBody.data(pages);
    }

    /**
     * 商品分类表保存
     * @param goodsCategoryDto 商品分类表实体类
     * @return
     */
    @ApiOperation("商品分类表保存")
    @PostMapping("/save")
    public RespBody<String> saveGoodsCategory(@RequestBody GoodsCategoryDto goodsCategoryDto){
        if(goodsCategoryDto == null ){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsCategoryEntity goodsCategoryEntity = ConvertUtil.transformObj(goodsCategoryDto,GoodsCategoryEntity.class);
        if(goodsCategoryService.save(goodsCategoryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品分类表编辑
     * @param goodsCategoryDto 商品分类表实体类
     * @return
     */
    @ApiOperation("商品分类表编辑")
    @PostMapping("/edit")
    public RespBody<String> updateGoodsCategory(@RequestBody GoodsCategoryDto goodsCategoryDto){
        if(goodsCategoryDto == null || goodsCategoryDto.getId() == null || goodsCategoryDto.getId().equals(0L)){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsCategoryEntity goodsCategoryEntity = ConvertUtil.transformObj(goodsCategoryDto,GoodsCategoryEntity.class);
        if(goodsCategoryService.updateById(goodsCategoryEntity)){
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品分类表删除
     * @param ids 商品分类表id集合
     * @return
     */
    @ApiOperation("商品分类表删除")
    @PostMapping("/delete")
    public RespBody<String> deleteGoodsCategory(@RequestBody List<Long> ids){
        if(ids == null || ids.isEmpty()){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
        if(goodsCategoryService.removeByIds(ids)) {
            return RespBody.data("保存成功");
        }else{
            return RespBody.fail(GlobalRespCode.OPERATE_ERROR);
        }
    }

    /**
     * 商品分类表详情
     * @param id 商品分类表id
     * @return
     */
    @ApiOperation("商品分类表详情")
    @GetMapping("/info")
    public RespBody<GoodsCategoryDto> infoGoodsCategory(@RequestParam Long id){
        if(id == null){
            throw new BizException(GlobalRespCode.PARAM_MISSED);
        }
            GoodsCategoryEntity goodsCategoryEntity = goodsCategoryService.getById(id);
            GoodsCategoryDto goodsCategoryDto = ConvertUtil.transformObj(goodsCategoryEntity,GoodsCategoryDto.class);
        return RespBody.data(goodsCategoryDto);
    }


}
